import org.apache.spark.rdd.RDD
import org.apache.spark.sql.{Row, SparkSession}
import org.apache.spark.sql.types.{DataTypes, StructField, StructType}

object dataframe2 {
  def main(args: Array[String]): Unit = {
    val spark=new SparkSession
    .Builder()
      .appName(s"${dataframe2.getClass.getSimpleName}")
      .master("location[*]")
      .enableHiveSupport()
      .getOrCreate()
    val rowRDD:RDD[Row] = spark.sparkContext.parallelize(List(
      new Student("车传广", 23, "男", "辽宁"),
      new Student("闫逾恒", 22, "男", "河北"),
      new Student("刘博", 20, "男", "天津"),
      new Student("王鑫达", 23, "男", "浙江"),
      new Student("田志", 23, "男", "河南")
    )).map(stu => {
      Row(stu.name, stu.age, stu.gender, stu.province)
    })

val schema=StructType(
  Array(
    StructField("name", DataTypes.StringType, true),
    StructField("age", DataTypes.IntegerType, true),
    StructField("gender", DataTypes.StringType, true),
    StructField("province", DataTypes.StringType, true)
  )
)
val df=spark.createDataFrame(rowRDD,schema )
df.printSchema()
    df.show()
    spark.stop()
  }
}
case class Student(val name: String, val age: Int, val gender: String, val province: String)
